package com.charles.seckillsystem.service.impl;

import com.charles.seckillsystem.schedule.DailyJob;
import com.charles.seckillsystem.service.SchedulerService;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Service;

@Service
public class SchedulerServiceImpl implements SchedulerService {
    
    @Override
    public void startDailyJob() throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = JobBuilder.newJob(DailyJob.class)
                .withIdentity("dailyJob")
                .build();

        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("dailyJobTrigger")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.repeatHourlyForever(24))
                .build();
        
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }
    
}
